package org.system.core.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.system.core.enums.ResultCodeEnum;

/**
 * @author baoRoot
 * @param <T>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

  private Integer code;
  private String message;
  private T data;

  protected static <T> Result<T> build(T data) {
    Result<T> result = new Result<T>();
    if (data != null){
      result.setData(data);
    }
    return result;
  }

  public static <T> Result<T> build(T data, Integer code, String message) {
    Result<T> result = build(data);
    result.setCode(code);
    result.setMessage(message);
    return result;
  }

  public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
    Result<T> result = build(data);
    result.setCode(resultCodeEnum.getCode());
    result.setMessage(resultCodeEnum.getMessage());
    return result;
  }

  public static<T> Result<T> ok(){
    return Result.ok(null);
  }

  public static<T> Result<T> ok(T data){
    return build(data, ResultCodeEnum.SUCCESS);
  }

  public static<T> Result<T> ok(Integer code, String message, T data) {
    return build(data, code, message);
  }

  public static<T> Result<T> ok(ResultCodeEnum resultCodeEnum, T data){
    return build(data, resultCodeEnum);
  }

  public static<T> Result<T> fail(){
    return Result.fail(null);
  }

  public static<T> Result<T> fail(T data){
    return build(data, ResultCodeEnum.FAIL);
  }

  public static<T> Result<T> fail(ResultCodeEnum resultCodeEnum, T data){
    return build(data, resultCodeEnum);
  }

  public static<T> Result<T> fail(Integer code, String message, T data){
    return build(data, code, message);
  }

}
